TEST = "<span style=text-align:left;float:left;>50 Runic Power<br></span><br><span style=text-align:right;float:right;>Melee Range</span><br><span style=text-align:left;float:left;>Instant<br></span><br><span style=text-align:left;float:left;>Requires Melee Weapon<br></span><br>Instantly strike the enemy, causing 100% weapon damage plus 6 as Frost damage."

local ENTITIES = {
	["&nbsp;"] = " ",
	["&gt;"] = ">",
	["&lt;"] = "<",
}

local function cleanup_html(src)
	local result = src:replace("\r", ""):replace("\n", ""):replace("<br>", "\n")
	for entity, char in pairs(ENTITIES) do
		result = result:replace(entity, char)
	end
	local e = result:len()
	while e >= 1 and (" \t\n"):find(result:sub(e, e), 1, 1) do
		e = e - 1
	end
	return result:sub(1, e)
end

local function conv_tip(tip)
	if not tip:find("<", 1, 1) then
		return cleanup_html(tip)
	end
	local r = {}
	local p = 1
	local pspan = false
	local m = tip:len() + 1
	while p < m do
		local s1, e1, style, v1 = tip:find("<span style=([^>]+)>(.-)</span>", p)
		local s2, e2, v2 = tip:find("<span>([^<]+)</span>", p)
		local s3, e3 = tip:find("<[bB][rR]/?>", p)
		if not s1 and not s2 and not s3 then break end
		s1 = s1 or m
		s2 = s2 or m
		s3 = s3 or m

		if s1 < s2 and s1 < s3 then
			local t = style:match("text%-align:([^;]+)")
			if t and t == "right" then
				if #r == 0 then
					r[1] = { left = "" }
				end
				r[#r].right = cleanup_html(v1)
			else
				local s = cleanup_html(v1)
				local c = style:match("color:([^;]+)")
				if c then
					s = "|cff"..c..s.."|r"
				end
				r[#r + 1] = { left = s }
			end
			p = e1 + 1
			pspan = true
		elseif s2 < s3 then
			r[#r + 1] = { left = cleanup_html(v2) }
			pspan = true
			p = e2 + 1
		else
			v = tip:sub(p, s3 - 1)
			if v:len() > 0 or not pspan then
				r[#r + 1] = { left = cleanup_html(v) }
			end
			pspan = false
			p = e3 + 1
		end
	end
	local v = cleanup_html(tip:sub(p, -1))

	if v:len() then
		r[#r + 1] = { left = v }
	end

	for i = #r, 1, -1 do
		if not r[i].right and r[i].left == "" then
			r[i] = nil
		else
			break
		end
	end
	return r
end

local r = conv_tip(TEST)
for i, v in ipairs(r) do
	if v.right then
		print(string.format("%.4d\t%q\t%q", i, v.left, v.right))
	else
		print(string.format("%.4d\t%q", i, v.left))
	end
end

